package com.learm.socket.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class NioSocketClient {
    
    public static void main(String[] args){
        NioSocketClient.client();
    }

    public static void client(){
        try {
            SocketChannel sChannel=SocketChannel.open(new InetSocketAddress("127.0.0.1",9898));//UDP方式
            FileChannel fileChannel= FileChannel.open(Paths.get("E://gc.log"), StandardOpenOption.READ);//打开传输通道
            ByteBuffer buf=ByteBuffer.allocate(1024);//申请缓冲区
            while(fileChannel.read(buf)!=-1){
                buf.flip();
                sChannel.write(buf);
                buf.clear();
            }

            sChannel.shutdownOutput();
            //接受服务端的反馈
            int len=0;
            while((len=sChannel.read(buf))!=-1){
                buf.flip();
                System.out.println(new String(buf.array(),0,len));
                buf.clear();
                break;
            }
            fileChannel.close();
            sChannel.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
